// internal/infrastructure/router/router.go
package router

import (
	"go-admin1/admin/system/handler"
	"go-admin1/internal/common/middleware"

	"github.com/gin-gonic/gin"
)

// SetupRouter 设置路由
func SetupRouter() *gin.Engine {
	r := gin.New()

	// 全局中间件
	r.Use(gin.Logger())
	r.Use(gin.Recovery())

	// API路由组
	api := r.Group("/api/v1")
	{
		// 不需要认证的路由
		public := api.Group("/")
		{
			captchaHandler := handler.NewCaptchaHandler()
			loginHandler := handler.NewLoginHandler()
			public.POST("login", loginHandler.Login)
			public.GET("captchaImage", captchaHandler.CaptchaImage)
		}

		// 需要认证的路由组
		protected := api.Group("")
		protected.Use(middleware.AuthMiddleware())
		{
			// 系统模块路由
			system := protected.Group("/sys")
			{
				userHandler := handler.NewUserHandler()
				loginHandler := handler.NewLoginHandler()
				system.GET("/users/:id", userHandler.GetUser)
				system.PUT("/users/:id", userHandler.UpdateUser)
				system.DELETE("/users/:id", userHandler.DeleteUser)
				system.GET("/users", userHandler.ListUsers)
				system.POST("/logout", loginHandler.Logout)
				system.GET("/userInfo", loginHandler.GetUserInfo)
				system.GET("/getRouters", loginHandler.GetRoutes)

			}
		}
	}

	return r
}
